define(function(require, exports, module){
	'use strict';
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/themes/color.css");
	require("resource/js/easyui/easyloader.js");
	require("resource/js/component/rules.js");
	var easyutil = require("resource/js/component/easyutil.js");
	var combobox = require("resource/js/component/easycombobox.js");
	var config = {
		type:"buss",
		name:"业务流程",
		column:[[
		     {field:'processKey',title:'流程编码',width:100,align:'center',height:50},
	         {field:'processNodeId',title:'节点编码',width:100,align:'center',height:50},
	         {field:'processNodeName',title:'节点名称',width:100,align:'center',height:50},
	         {field:'refKey',title:'节点字段编号',width:100,align:'center',height:50},
	         {field:'maxApprover',title:'审批人数量',width:100,align:'center',height:50,formatter:function(val){
	        	 return val == -1?"不限":val;
	         }},
	         {field:'isSequential',title:'串行/并行',width:100,align:'center',height:50,formatter:function(val){
	        	 return val == 0?"串行":"并行";
	         }},
//	         {field:'seal',title:'印章',width:100,align:'center',height:50,formatter:function(value,row,index){
//	        	 if(row.processNodeId !== 'PROCESS_START'){
//	        		 var html = '';
//	        		 if(row.isSeal == '1'){
//	        			 html += '<input type="checkbox" role="seal" checked="checked" id="seal_'+index+'" class="chooseBtn" />';
//	        		 }else{
//	        			 html +='<input type="checkbox" role="seal" id="seal_'+index+'" class="chooseBtn" />';
//	        		 }
//		        	 return  html+'<label for="seal_'+index+'" class="choose-label"></label>'
//		        	
//	        	 }
//	         }},
//	         {field:'sign',title:'签名',width:100,align:'center',height:50,formatter:function(value,row,index){
//	        	 if(row.processNodeId !== 'PROCESS_START'){
//	        		 var html = '';
//	        		 if(row.isSign == '1'){
//	        			 html += '<input role="sign" type="checkbox" checked="checked" id="sign_'+index+'" class="chooseBtn" />';
//	        		 }else{
//	        			 html +='<input role="sign" type="checkbox" id="sign_'+index+'" class="chooseBtn" />';
//	        		 }
//		        	 return  html+'<label for="sign_'+index+'" class="choose-label"></label>'
//		        	
//	        	 }
//	         }}
		]]
	};
	var dgutil = undefined;
	var datagridconfig = undefined;
	var processKey = '';
	function initPage(){
		var urlTool = require("resource/js/component/urlTool.js");
		processKey = urlTool.getParam("processKey");
		$("#processKey").val(processKey);
		$("#businessId").val(urlTool.getParam("businessId"));
		//初始化主表datagrid
		dgutil = new easyutil.ajaxDataGrid(datagridconfig);
//		$('body').on('change','.chooseBtn',function(){
//			var index =Number(this.id.split('_')[1]);
//			$('#dg').datagrid('selectRow',index);
//			var row = $('#dg').datagrid('getSelected');
//			var flag = Number(this.checked).toString();
//			var role = $(this).attr('role');
//			var param = {
//					processNodeId:	row.processNodeId,
//					processKey:row.processKey
//			}
//			if('seal' === role){
//				param.isSeal = flag;
//			}else if('sign' === role){
//				param.isSign = flag;
//			}else{
//				return
//			}
//
//			$('#dg').datagrid('loading');
//			easyutil.ajaxHandler(
//					BASEPATH+'/oa/act/actExBussDef/updateActEExBussDef.htm',
//					param,
//					function(){
//						$('#dg').datagrid('loaded');
//					});
//		});
	}
	var initViewColsData = function(processKey,processNodeId){
		$("#clos_processKey").val(processKey);
		$("#cols_processNodeId").val(processNodeId);
		//查询当前节点的表单字段并展示
		easyutil.ajaxHandler(BASEPATH+'/act/actExBussDef/queryNodeTableAndCols.htm',$("#colsform").serializeJson(),function(code,msg,data){
			if(data==undefined||data==null){
				var table_html="<table class='table-col-row'></table>";
				$("#search_cols").html(table_html);
			}else{
				var table_html="<table class='table-col-row'>";
				for(var i in data){
					var table_data = {tableKey:data[i].table_key,operType:data[i].oper_type,isMore:data[i].ismore,cols:[]};
					var tableName = data[i].table_key+"("+data[i].short_name_ch+")";
					table_html +="<tr class='head-tr' tb='"+i+"'><td style='display:none'>"+JSON.stringify(table_data)+"</td><td>表名："+tableName+"</td><td>操作类型："+data[i].opertypename+"</td><td>是否多条："+data[i].ismorename+"</td></tr>"
					var cols = data[i].cols;
					for(var j in cols){
						var cache_cols = {colKey:cols[j].col_key,colName:cols[j].col_name};
						table_html += "<tr tb='"+i+"'><td style='display:none'>"+JSON.stringify(cache_cols)+"</td><td>"+cols[j].col_name+"</td><td>"+cols[j].col_name_ch+"</td><td><a href='javascript:void(0);' class='delete-row'>删除</a></td></tr>";
					}
				}
				table_html+="</table>";
				$("#search_cols").html("<div class='table-div'>"+table_html+"</div>");
			}
			$(".delete-row").bind('click',function(e){
				if($(this).parent().parent().prev("tr").attr("class")=='head-tr' 
					&& ($(this).parent().parent().next("tr").attr("class")=='head-tr'
					|| 	$(this).parent().parent().next("tr").length == 0
					)){
						var tb = $(this).attr('tb');
						$(this).parent().parent().prev().remove();
				}
				$(this).parent().parent().remove();
			});
		});
	}
	function editCols(baseUrl,row){
		$("#colsdlg").dialog({
			iconCls: 'icon-save',
			title:'',
			href:baseUrl,
			onLoad:function(){
				table_col = [];
				$("#cols_processKey").val(processKey);
				$("#cols_processNodeId").val(row.processNodeId);
				$("#old_refKey").val(row.refKey);
				initViewColsData(row.processKey,row.processNodeId);
			},
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						table_col = [];
						if($("#colsform").form("validate")){
							if($(".head-tr") == undefined||$(".head-tr") == null){
								$.messager.alert('','请添加字段信息。');
							}else{
								$(".head-tr").each(function(i,e){
									var index = $(e).attr("tb");
									$("tr[tb="+index+"]").each(function(j,e){
										var str = $(e).find("td").first().text();
										if($(e).attr("class")=='head-tr'){
											var obj = JSON.parse(str);
											table_col.push(obj);
										}else{
											var obj = JSON.parse(str);
											table_col[i].cols.push(obj);
										}
									});
								});
							}
							var req_param = $("#colsform").serializeJson();
							req_param.details=JSON.stringify(table_col);
							easyutil.ajaxHandler(BASEPATH+'/act/actExBussDef/saveActExBussDef.htm',req_param,function(){
								dgutil.pager();
								$("#colsdlg").dialog("close");
							});
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#colsdlg').dialog('close');
				}
			}]
		});
		$("#colsdlg").dialog("open");
	}
	module.exports = {
		trans:function(id){
			var buttons ={ 
				title:{
					_class:"",
					text:"业务流程"
				},
				btn:[
			       {
				iconCls: 'icon-edit',
				//addClass:'l-btn-display',
				text:"编辑表单及字段",
				url:'/view/oa/act/ex/bussDef/addCols',
				needRow:true,
				handler: editCols
			}
			]};
			datagridconfig = {
					fitColumns:true,
					singleSelect:true,
					striped:true,
					autoRowHeight:false,
					fit: true,
					url:BASEPATH+'/oa/act/actExBussDef/queryActAllBussDef.htm',
					toolbar:buttons,
					columns:config.column,
					eid:"dg",
					args:"searchform"
			};
			using(["datagrid","parser","textbox","dialog","combobox","messager"],function(){
				initPage();
			});
			
		}
	}
});